home *** CD-ROM | disk | FTP | other *** search
- unit ugltest;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- OpenGL;
-
- type
- TForm1 = class(TForm)
- procedure FormCreate(Sender: TObject);
- procedure FormPaint(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure FormResize(Sender: TObject);
- private
- { Private declarations }
- hRC: hGLRC; // permanent Rendering context
- dc: hDC; // private GDI Device context
- hPal: hPalette; // global palette handle
- procedure WMQueryNewPalette (var Message: TWMQueryNewPalette); message WM_QueryNewPalette;
- procedure WMPaletteChanged (var Message: TWMPaletteChanged); message WM_PaletteChanged;
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- // Select the pixel format for a given device context
-
- procedure SetDCPixelFormat (dc: hDC);
- var
- PixelFormat: Integer;
- pfd: TPixelFormatDescriptor;
- begin
- FillChar (pfd, sizeof (pfd), 0);
- with pfd do begin
- nSize := sizeof (pfd);
- nVersion := 1;
- dwFlags := PFD_Draw_To_Window or PFD_DoubleBuffer or PFD_Support_OpenGL;
- iPixelType := PFD_Type_RGBA;
- cColorBits := 24;
- cDepthBits := 32;
- iLayerType := PFD_Main_Plane;
- end;
-
- // Choose a pixel format that best matches that described in pfd
- PixelFormat := ChoosePixelFormat (dc, @pfd);
- // Set the pixel format for the device context
- SetPixelFormat (dc, PixelFormat, @pfd);
- end;
-
- // If necessary, creates a 3-3-2 palette for the device context listed.
-
- function GetOpenGLPalette (dc: hDC): hPalette;
- var
- pPal: ^TLogPalette;
- pfd: TPixelFormatDescriptor;
- i, Colors, PixelFormat: Integer;
- RedRange,GreenRange,BlueRange: Byte;
- begin
- Result := 0;
- // Get the pixel format index and retrieve the pixel format description
- PixelFormat := GetPixelFormat (dc);
- DescribePixelFormat (dc, PixelFormat, sizeof (pfd), pfd);
- // Does this pixel format require a palette?
- // If not, do not create a palette and just return 0
- if (pfd.dwFlags and PFD_Need_Palette) = 0 then Exit;
- // Number of entries in palette. 8 bits yeilds 256 entries
- Colors := 1 shl pfd.cColorBits;
- // Allocate space for a logical palette structure + palette entries
- GetMem (pPal, sizeof (TLogPalette) + (Colors * sizeof (TPaletteEntry)));
- try
- // Fill in palette header
- pPal^.palVersion := $300;
- pPal^.palNumEntries := Colors;
- // Build mask of all 1's. This creates a number represented by having
- // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
- // pfd.cBlueBits.
- RedRange := (1 shl pfd.cRedBits) - 1;
- GreenRange := (1 shl pfd.cGreenBits) - 1;
- BlueRange := (1 shl pfd.cBlueBits) - 1;
- // Loop through all the palette entries
- for i := 0 to Colors - 1 do begin
- // Fill in the 8-bit equivalents for each component
- pPal^.palPalEntry[i].peRed := (i shr pfd.cRedShift) and RedRange;
- pPal^.palPalEntry[i].peRed := Trunc(pPal^.palPalEntry[i].peRed * 255.0 / RedRange);
- pPal^.palPalEntry[i].peGreen := (i shr pfd.cGreenShift) and GreenRange;
- pPal^.palPalEntry[i].peGreen := Trunc(pPal^.palPalEntry[i].peGreen * 255.0 / GreenRange);
- pPal^.palPalEntry[i].peBlue := (i shr pfd.cBlueShift) and BlueRange;
- pPal^.palPalEntry[i].peBlue := Trunc(pPal^.palPalEntry[i].peBlue * 255.0 / BlueRange);
- pPal^.palPalEntry[i].peFlags := 0;
- end;
- // Create the palette
- Result := CreatePalette (pPal^);
- // Select and realize the palette for this device context
- SelectPalette (dc, Result, False);
- RealizePalette (dc);
- finally
- // Free the memory used for the logical palette structure
- FreeMem (pPal, sizeof (TLogPalette) + (Colors * sizeof (TPaletteEntry)));
- end;
- end;
-
- // Perform any needed initialization on the rendering context.
- // Here it sets up and initializes the lighting for the scene.
-
- procedure SetupRC (dc: hDC);
- const
- // Light values and coordinates
- whiteLight: array [0..3] of GLfloat = ( 0.4, 0.4, 0.4, 1.0 );
- diffuseLight: array [0..3] of GLfloat = ( 0.8, 0.8, 0.8, 1.0 );
- specular: array [0..3] of GLfloat = ( 0.9, 0.9, 0.9, 1.0 );
- lightPos: array [0..3] of GLfloat = ( -100.0, 200.0, 50.0, 1.0 );
- var
- font: hFont;
- logFont: TLogFont;
- agmf: array [0..127] of TGlyphMetricsFloat;
- begin
- // Setup the Font characteristics
- FillChar (logfont, sizeof (logfont), 0);
- logfont.lfHeight := -10;
- logfont.lfWeight := FW_Bold;
- logfont.lfCharSet := Ansi_CharSet;
- logfont.lfOutPrecision := Out_Default_Precis;
- logfont.lfClipPrecision := Clip_Default_Precis;
- logfont.lfQuality := Default_Quality;
- logfont.lfPitchAndFamily := Default_Pitch;
- lstrcpy (logfont.lfFaceName, 'Times New Roman');
- // Create the font and display list
- Font := CreateFontIndirect (logfont);
- try
- SelectObject (dc, Font);
- // create display lists for glyphs with 0.1 extrusion and default
- // deviation. The display list numbering starts at 1000 (it could
- // be any number)
- wglUseFontOutlines (dc, 0, 128, 1000, 0.0, 1.0, WGL_Font_Polygons, @agmf);
- finally
- DeleteObject (Font);
- end;
-
- glEnable (gl_Depth_Test); // Hidden surface removal
- glEnable (gl_Color_Material);
- glClearColor (0.0, 0.0, 0.0, 1.0);
- glEnable (gl_Lighting);
- glLightfv (gl_Light0, gl_Ambient, @whiteLight);
- glLightfv (gl_Light0, gl_Diffuse, @diffuseLight);
- glLightfv (gl_Light0, gl_Specular, @specular);
- glLightfv (gl_Light0, gl_Position, @lightPos);
- glEnable (gl_Light0);
-
- glColorMaterial (gl_Front, gl_Ambient_And_Diffuse);
- glMaterialfv (gl_Front, gl_Specular, @specular);
- glMateriali (gl_Front, GL_Shininess, 128);
-
- // Green 3D Text
- glColor3ub (0, 255, 0);
- // Black background
- glClearColor (0.0, 0.0, 0.0, 1.0);
- end;
-
- procedure TForm1.FormCreate (Sender: TObject);
- begin
- // Window creation, setup for OpenGL
- dc := GetDC (Handle);
- SetDCPixelFormat (dc);
- hPal := GetOpenGLPalette (dc);
- // Create the rendering context and make it current
- hRC := wglCreateContext (dc);
- wglMakeCurrent (dc, hRC);
- SetupRC (dc);
- end;
-
- procedure RenderScene (const TheText: String);
- begin
- // Clear the window with current clearing color
- glClear (gl_Color_Buffer_Bit or gl_Depth_Buffer_Bit);
- // Save the matrix state and do the rotations
- glMatrixMode (gl_ModelView);
- // Draw the string
- glListBase (1000);
- glPushMatrix;
- glRotatef (6.0, 1.0, 0.0, 0.0);
- glRotatef (350.0, 0.0, 1.0, 0.0);
- glCallLists (Length (TheText), gl_Unsigned_Byte, PChar (TheText));
- glPopMatrix;
- // Flush drawing commands
- glFlush;
- end;
-
- procedure TForm1.WMQueryNewPalette (var Message: TWMQueryNewPalette);
- begin
- Inherited;
- if hPal <> 0 then begin
- // Selects the palette into the current device context
- SelectPalette (dc, hPal, False);
- // Map entries from current palette to system palette
- RealizePalette (dc);
- // Repaint, forces remap of palette in current window
- InvalidateRect (Handle, Nil, False);
- end;
- end;
-
- procedure TForm1.WMPaletteChanged (var Message: TWMPaletteChanged);
- begin
- Inherited;
- // Don't do anything if the palette does not exist, or if
- // this is the window that changed the palette.
- if (hPal <> 0) and (Handle <> Message.PalChg) then begin
- // Select the palette into the device context
- SelectPalette (dc, hPal, False);
- // Map entries to system palette
- RealizePalette (dc);
- // Remap the current colors to the newly realized palette
- UpdateColors (dc);
- end;
- end;
-
- procedure TForm1.FormPaint(Sender: TObject);
- begin
- // The painting function. This message sent by Windows
- // whenever the screen needs updating.
- RenderScene ('Delphi 3.0');
- SwapBuffers (dc);
- ValidateRect (Handle, Nil);
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- // Deselect the current rendering context and delete it
- wglMakeCurrent (dc, 0);
- wglDeleteContext (hRC);
- if hPal <> 0 then DeleteObject (hPal);
- ReleaseDC (Handle, dc);
- end;
-
- // Change viewing volume and viewport. Called when window is resized
-
- procedure ChangeSize (w, h: GLsizei);
- const
- nRange: GLFloat = 125.0;
- begin
- // Prevent a divide by zero
- if h = 0 then h := 1;
- // Set Viewport to window dimensions
- glViewport (0, 0, w, h);
- // Reset coordinate system
- glMatrixMode (gl_Projection);
- glLoadIdentity;
- // Establish clipping volume (left, right, bottom, top, near, far)
- if w <= h then glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange*2.0, nRange*2.0)
- else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange*2.0, nRange*2.0);
- // Set up transformation to draw the string.
- glTranslatef (-110.0, 0.0, -5.0);
- glScalef(60.0, 60.0, 60.0);
- glMatrixMode (gl_ModelView);
- glLoadIdentity;
- end;
-
- procedure TForm1.FormResize (Sender: TObject);
- begin
- ChangeSize (ClientWidth, ClientHeight);
- InvalidateRect (Handle, Nil, False);
- end;
-
- end.
-
-
-